﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ESRI.ArcGIS.Client;
using Eaglight.Behaviors;
using ESRI.ArcGIS.Client.Symbols;
using LightControls;

namespace Eaglight
{
    public class DrawToolbar: Toolbar
    {

        private GraphicsLayer glayer;

        private LcImagePushRadioButton btnFreehand;
        private LcImagePushRadioButton btnPoint;
        private LcImagePushRadioButton btnPolygon;
        private LcImagePushRadioButton btnPolyline;
        private LcImagePushRadioButton btnRectangle;
        private LcImagePushRadioButton btnSelect;
        private LcImageButton btnClear;
        private DrawFreehandBehavior drawFreehandBehavior = new DrawFreehandBehavior();
        private DrawPointBehavior drawPointBehavior = new DrawPointBehavior();
        private DrawPolygonBehavior drawPolygonBehavior = new DrawPolygonBehavior();
        private DrawPolylineBehavior drawPolylineBehavior = new DrawPolylineBehavior();
        private DrawRectangleBehavior drawRectangleBehavior = new DrawRectangleBehavior();
        private SelectGraphicBehavior selectGraphicBehavior = new SelectGraphicBehavior();

        private SimpleMarkerSymbol hotMarkerSymbol;
        private SimpleLineSymbol hotLineSymbol;
        private SimpleFillSymbol hotFillSymbol;

        private MarkerSymbol drawMarkerSymbol;
        private LineSymbol drawLineSymbol;
        private FillSymbol drawFillSymbol;

        public DrawToolbar()
        {
            DefaultStyleKey = typeof(DrawToolbar);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            btnFreehand = (LcImagePushRadioButton)base.GetTemplateChild("btnFreehand");
            btnPoint = (LcImagePushRadioButton)base.GetTemplateChild("btnPoint");
            btnPolygon = (LcImagePushRadioButton)base.GetTemplateChild("btnPolygon");
            btnPolyline = (LcImagePushRadioButton)base.GetTemplateChild("btnPolyline");
            btnRectangle = (LcImagePushRadioButton)base.GetTemplateChild("btnRectangle");
            btnSelect = (LcImagePushRadioButton)base.GetTemplateChild("btnSelect");
            btnClear = (LcImageButton)base.GetTemplateChild("btnClear");

            controlsInitialized = true;

            if (ShowToolTips == true)
                enableToolTips();
            else
                disableToolTips();


            initEvents();
            initSymbols();

            
        }

        private void initSymbols()
        {
            hotMarkerSymbol = (SimpleMarkerSymbol)((StackPanel)GetTemplateChild("LayoutRoot")).Resources["DrawHotMarkerSymbol"];
            hotLineSymbol = (SimpleLineSymbol)((StackPanel)GetTemplateChild("LayoutRoot")).Resources["DrawHotLineSymbol"];
            hotFillSymbol = (SimpleFillSymbol)((StackPanel)GetTemplateChild("LayoutRoot")).Resources["DrawHotFillSymbol"];
            drawMarkerSymbol = (MarkerSymbol)((StackPanel)GetTemplateChild("LayoutRoot")).Resources["SelectMarkerSymbol"];
            drawLineSymbol = (LineSymbol)((StackPanel)GetTemplateChild("LayoutRoot")).Resources["SelectLineSymbol"];
            drawFillSymbol = (FillSymbol)((StackPanel)GetTemplateChild("LayoutRoot")).Resources["SelectFillSymbol"];
        }

        private void initEvents()
        {
            btnPolygon.Checked += new RoutedEventHandler(btnPolygon_Checked);
            btnPolygon.Unchecked += new RoutedEventHandler(btnPolygon_Unchecked);
            btnPolyline.Checked += new RoutedEventHandler(btnPolyline_Checked);
            btnPolyline.Unchecked += new RoutedEventHandler(btnPolyline_Unchecked);
            btnPoint.Checked += new RoutedEventHandler(btnPoint_Checked);
            btnPoint.Unchecked += new RoutedEventHandler(btnPoint_Unchecked);
            btnFreehand.Checked += new RoutedEventHandler(btnFreehand_Checked);
            btnFreehand.Unchecked += new RoutedEventHandler(btnFreehand_Unchecked);
            btnRectangle.Checked += new RoutedEventHandler(btnRectangle_Checked);
            btnRectangle.Unchecked += new RoutedEventHandler(btnRectangle_Unchecked);
            btnSelect.Checked += new RoutedEventHandler(btnSelect_Checked);
            btnSelect.Unchecked += new RoutedEventHandler(btnSelect_Unchecked);
            btnClear.Click += new RoutedEventHandler(btnClear_Click);
        }

        void btnSelect_Unchecked(object sender, RoutedEventArgs e)
        {
            selectGraphicBehavior.Detach();
        }

        void btnSelect_Checked(object sender, RoutedEventArgs e)
        {
            
            GraphicsLayer gl = (GraphicsLayer)Map.Layers[GraphicsLayer];
            selectGraphicBehavior.Attach(gl);
        }

        void btnRectangle_Unchecked(object sender, RoutedEventArgs e)
        {
            drawRectangleBehavior.Detach();
        }

        void btnRectangle_Checked(object sender, RoutedEventArgs e)
        {
            drawRectangleBehavior.GraphicsLayer = Map.Layers[GraphicsLayer] as GraphicsLayer;
            drawRectangleBehavior.HotSymbol = hotFillSymbol;
            drawRectangleBehavior.Symbol = drawFillSymbol;
            drawRectangleBehavior.Attach(Map);
        }

        void btnFreehand_Unchecked(object sender, RoutedEventArgs e)
        {
            drawFreehandBehavior.Detach();
        }

        void btnFreehand_Checked(object sender, RoutedEventArgs e)
        {
            drawFreehandBehavior.GraphicsLayer = Map.Layers[GraphicsLayer] as GraphicsLayer;
            drawFreehandBehavior.HotSymbol = hotLineSymbol;
            drawFreehandBehavior.Symbol = drawLineSymbol;
            drawFreehandBehavior.Attach(Map);
        }

        void btnPoint_Unchecked(object sender, RoutedEventArgs e)
        {
            drawPointBehavior.Detach();
        }

        void btnPoint_Checked(object sender, RoutedEventArgs e)
        {
            drawPointBehavior.GraphicsLayer = Map.Layers[GraphicsLayer] as GraphicsLayer;
            drawPointBehavior.HotSymbol = hotMarkerSymbol;
            drawPointBehavior.Symbol = drawMarkerSymbol;
            drawPointBehavior.Attach(Map);
        }

        void btnPolyline_Unchecked(object sender, RoutedEventArgs e)
        {
            drawPolylineBehavior.Detach();
        }

        void btnPolyline_Checked(object sender, RoutedEventArgs e)
        {
            drawPolylineBehavior.GraphicsLayer = Map.Layers[GraphicsLayer] as GraphicsLayer;
            drawPolylineBehavior.HotSymbol = hotLineSymbol;
            drawPointBehavior.Symbol = drawLineSymbol;
            drawPolylineBehavior.Attach(Map);
        }

        void btnPolygon_Unchecked(object sender, RoutedEventArgs e)
        {
            drawPolygonBehavior.Detach();
        }

        void btnPolygon_Checked(object sender, RoutedEventArgs e)
        {
            drawPolygonBehavior.GraphicsLayer = Map.Layers[GraphicsLayer] as GraphicsLayer;
            drawPolygonBehavior.HotSymbol = hotFillSymbol;
            drawPolygonBehavior.Symbol = drawFillSymbol;
            drawPolygonBehavior.Attach(Map);
            
        }

        void btnClear_Click(object sender, RoutedEventArgs e)
        {
            GraphicsLayer gl = Map.Layers[GraphicsLayer] as GraphicsLayer;
            gl.ClearGraphics();
        }

        protected override void enableToolTips()
        {
            if (controlsInitialized)
            {
                ToolTipService.SetToolTip(btnFreehand, "Free Hand Draw");
                ToolTipService.SetToolTip(btnPoint, "Draw Point");
                ToolTipService.SetToolTip(btnPolygon, "Draw Polygon");
                ToolTipService.SetToolTip(btnPolyline, "Draw Polyline");
                ToolTipService.SetToolTip(btnClear, "Clear Graphics");
                ToolTipService.SetToolTip(btnRectangle, "Draw Rectangle");
                ToolTipService.SetToolTip(btnSelect, "Select Graphic");
            }
        }

        protected override void disableToolTips()
        {
            if (controlsInitialized)
            {
                ToolTipService.SetToolTip(btnFreehand, null);
                ToolTipService.SetToolTip(btnPoint, null);
                ToolTipService.SetToolTip(btnPolygon, null);
                ToolTipService.SetToolTip(btnPolyline, null);
                ToolTipService.SetToolTip(btnClear, null);
                ToolTipService.SetToolTip(btnRectangle, null);
                ToolTipService.SetToolTip(btnSelect, null);               
            }
        }

        public static readonly DependencyProperty GraphicsLayerProperty = DependencyProperty.Register("GraphicsLayer", typeof(string), typeof(DrawToolbar), null);
        public static readonly DependencyProperty MarkerSymbolProperty = DependencyProperty.Register("MarkerSymbol", typeof(MarkerSymbol), typeof(DrawToolbar), null);
        public static readonly DependencyProperty LineSymbolProperty = DependencyProperty.Register("LineSymbol", typeof(LineSymbol), typeof(DrawToolbar), null);
        public static readonly DependencyProperty FillSymbolProperty = DependencyProperty.Register("FillSymbol", typeof(FillSymbol), typeof(DrawToolbar), null);
        public static readonly DependencyProperty UseCustomSymbolsProperty = DependencyProperty.Register("UseCustomSymbols", typeof(bool), typeof(DrawToolbar), null);
        public static readonly DependencyProperty MapProperty = DependencyProperty.Register("Map", typeof(Map), typeof(DrawToolbar), null);

        public Map Map
        {
            get
            {
                return (Map)GetValue(MapProperty);
            }
            set
            {
                SetValue(MapProperty, value);
            }
        }



        public String GraphicsLayer
        {
            get 
            { 
                return (string)GetValue(GraphicsLayerProperty); 
            }
            set 
            { 
                SetValue(GraphicsLayerProperty, value); 
            }
        }

        public MarkerSymbol MarkerSymbol
        {
            get 
            {
                return (MarkerSymbol)GetValue(MarkerSymbolProperty);
            }
            set
            {
                SetValue(MarkerSymbolProperty, value);
            }
        }

        public LineSymbol LineSymbol
        {
            get
            {
                return (LineSymbol)GetValue(MarkerSymbolProperty);
            }
            set
            {
                SetValue(MarkerSymbolProperty, value);
            }
        }

        public FillSymbol FillSymbol
        {
            get
            {
                return (FillSymbol)GetValue(MarkerSymbolProperty);
            }
            set
            {
                SetValue(MarkerSymbolProperty, value);
            }
        }

        public bool UseCustomSymbols
        {
            get
            {
                return (bool)GetValue(UseCustomSymbolsProperty);
            }
            set
            {
                SetValue(UseCustomSymbolsProperty, value);
            }
        }

        
    }
}
